import JsEncrypt from "jsencrypt";
function rsaEncrypt(params: object | string,publicKey:string): string | false {
    const Encrypt = new JsEncrypt();
    Encrypt.setPublicKey(publicKey);
    return Encrypt.encrypt(
      typeof params === 'object' ? JSON.stringify(params) : params,
    );
  }

export{rsaEncrypt}

const data = { // 要排序的原始对象
    name: '张三',
    age: 18,
    sex: '男',
    height: '189cm',
    weight: '80kg',
    hobby: ['跑步', '篮球', '钓鱼'],
    app:{
        id:12,
        name:"app",
        child:{
            a:1,
            b:2,
            c:3
        }
    }
}

// const sortData = { } // 排序后的对象
// Object.keys(data).sort().map(key => {
//     sortData[key] = data[key]
// })
// console.info(JSON.stringify(sortData)) // => {"age":18,"height":"189cm","hobby":["跑步","篮球","钓鱼"],"name":"张三","sex":"男","weight":"80kg"}

// const reverseData = { } // 倒序后的对象
// Object.keys(data).sort().reverse().map(key => {
//     reverseData[key] = data[key]
// })
// console.info(JSON.stringify(reverseData)) // => {"weight":"80kg","sex":"男","name":"张三","hobby":["跑步","篮球","钓鱼"],"height":"189cm","age":18}


function getSign(base,data){
    const sortData = { } // 排序后的对象
    Object.keys(data).sort().forEach(key => {
        sortData[key] = data[key]
    })
    let signStr="";
    Object.keys(sortData).forEach((value,index,arr)=>{
        console.log(base,sortData)
        if(!sortData[value])return;
        if(base){
            signStr=signStr.concat(signStr,base,"_")
        }
        if(sortData[value].constructor===Array){
            signStr=signStr.concat(value,"=",sortData[value].join(","),"&")
            return
        }
        if(sortData[value].constructor===Object){
            signStr=signStr.concat(getSign(value,sortData[value]))
            return
        }
        signStr=signStr.concat(value,"=",sortData[value],"&")
    })
    return signStr.substring(0,signStr.length)
}

const signStr=""
console.log(getSign("",data))
